package org.nf.trading.controller.app.circle;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nf.trading.controller.BaseController;
import org.nf.trading.dto.PostCommentDTO;
import org.nf.trading.dto.PostDTO;
import org.nf.trading.entity.Post;
import org.nf.trading.service.app.circle.PostService;
import org.nf.trading.vo.ResultVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/api/post")
@CrossOrigin
public class PostController extends BaseController {
    private final PostService postService;

    /**
     * 通过用户id查询帖子的数量
     * @param uid
     * @return
     */
    @GetMapping("/count/{uid}")
    public ResultVO countPostByUid(@PathVariable("uid") Integer uid){
        Long l = postService.countPostByUid(uid);
        return success(l);
    }

    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    public ResultVO getPostList(@PathVariable("pageNum") Integer pageNum
                                , @PathVariable("pageSize") Integer pageSize){
        return success(postService.listPosts(pageNum, pageSize));
    }

    /**
     * 通过帖子id查询一个帖子(包含用户信息和评论信息)
     * @param postId
     * @return
     */
    @GetMapping("{postId}")
    public ResultVO getPostById(@PathVariable("postId") Integer postId){
        PostCommentDTO postCommentDTO = postService.getPostById(postId);
        return success(postCommentDTO);
    }

    /**
     * 查询全部帖子，用户id
     * @return
     */
    @GetMapping("pageNum/{pageNum}/pageSize/{pageSize}/userId/{userId}")
    public ResultVO getPostAll(
            @PathVariable("userId") Integer userId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize){
        List<PostDTO> postDTOS = postService.listPostsUid(userId, pageNum, pageSize);
        return success(postDTOS);
    }

    /**
     * 发布帖子
     * @param post
     * @return
     */
    @PostMapping
    public ResultVO insert(Post post, MultipartFile[] files){
        boolean insert = postService.insert(post,files);
        return success(200,"发布成功",insert);
    }
}
